package com.hly.musicbackground.model.boots.rest;

import com.hly.musicbackground.Final.FictionShelfFinal;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.domain.Fiction;
import com.hly.musicbackground.domain.FictionChapter;
import com.hly.musicbackground.model.boots.VO.FictionChapterListVO;
import com.hly.musicbackground.model.boots.service.FictionChapterService;
import com.hly.musicbackground.model.boots.service.FictionService;
import com.hly.musicbackground.model.boots.service.FictionShelfService;
import com.hly.musicbackground.result.Code;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 小说目录
 * */
@RestController
@RequestMapping("/api/fictionChapter")
@Api(tags = "小说目录")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FictionChapterController {

    private final FictionChapterService fictionChapterService;
    private final FictionService fictionService;
    private final FictionShelfService fictionShelfService;


    /**
     * 获取小说目录
     * @param id 小说id
     * */
    @GetMapping("/list/{id}")
    @ApiOperation(value = "获取小说目录")
    public Result<FictionChapterListVO> detail(@PathVariable("id") int id){
        Fiction byId = fictionService.getById(id);
        if (byId == null){
            Code.BUSINESS_ERR("不存在该小说");
        }
        int userId = RequestContextUtils.getUser().getUserId();
        List<FictionChapter> list = fictionChapterService.getAutoFictionChapterList(byId);
        boolean bookshelf = fictionShelfService.isBookshelf(userId, id, FictionShelfFinal.FICTION_SHELF);
        FictionChapterListVO fictionChapterListVO = new FictionChapterListVO(list, bookshelf);
        return Result.ok(fictionChapterListVO);
    }
}
